/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utils;

import java.util.Calendar;
import java.util.Hashtable;

/**
 *
 * @author cristian
 */
public class Datetime {

    private final static String SEPARATOR = " ";
    private final static String DATE_SEPARATOR = "/";
    private final static String TIME_SEPARATOR = "/";
    private final static String DATE_TIME_SEPARATOR = " - ";

    private final static Hashtable DAYS_OF_THE_WEEK = new Hashtable(7);
    private final static Hashtable MONTHS = new Hashtable(12);

    private Calendar calendar;
    private StringBuffer buffer = new StringBuffer();

    static {
        DAYS_OF_THE_WEEK.put("Sun", new Integer(Calendar.SUNDAY));
        DAYS_OF_THE_WEEK.put("Mon", new Integer(Calendar.MONDAY));
        DAYS_OF_THE_WEEK.put("Tue", new Integer(Calendar.TUESDAY));
        DAYS_OF_THE_WEEK.put("Wed", new Integer(Calendar.WEDNESDAY));
        DAYS_OF_THE_WEEK.put("Thu", new Integer(Calendar.THURSDAY));
        DAYS_OF_THE_WEEK.put("Fri", new Integer(Calendar.FRIDAY));
        DAYS_OF_THE_WEEK.put("Sat", new Integer(Calendar.SATURDAY));

        MONTHS.put("Jan", new Integer(Calendar.JANUARY));
        MONTHS.put("Feb", new Integer(Calendar.FEBRUARY));
        MONTHS.put("Mar", new Integer(Calendar.MARCH));
        MONTHS.put("Apr", new Integer(Calendar.APRIL));
        MONTHS.put("May", new Integer(Calendar.MAY));
        MONTHS.put("Jun", new Integer(Calendar.JUNE));
        MONTHS.put("Jul", new Integer(Calendar.JULY));
        MONTHS.put("Aug", new Integer(Calendar.AUGUST));
        MONTHS.put("Sep", new Integer(Calendar.SEPTEMBER));
        MONTHS.put("Oct", new Integer(Calendar.OCTOBER));
        MONTHS.put("Nov", new Integer(Calendar.NOVEMBER));
        MONTHS.put("Dec", new Integer(Calendar.DECEMBER));
    }

    //Sat Jan 24 22:14:29 +0000 2009
    public Datetime(String str) {
        String[] arr = StringUtils.split(str, SEPARATOR);
        calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, ((Integer)DAYS_OF_THE_WEEK.get(arr[0])).intValue());
        calendar.set(Calendar.MONTH, ((Integer)MONTHS.get(arr[1])).intValue());
        calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(arr[2]));
        String time = arr[3];
        time = time.substring(0, time.lastIndexOf((int)':'));
        String[] t = StringUtils.split(time, ":");
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(t[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(t[1]));
        calendar.set(Calendar.YEAR, Integer.parseInt(arr[arr.length-1]));
    }

    public String format() {
        resetBuffer();
        
        buffer.append(calendar.get(Calendar.DAY_OF_MONTH));
        buffer.append(DATE_SEPARATOR);
        buffer.append(calendar.get(Calendar.MONTH));
        buffer.append(DATE_SEPARATOR);
        buffer.append(calendar.get(Calendar.YEAR));
        buffer.append(DATE_TIME_SEPARATOR);
        buffer.append(calendar.get(Calendar.HOUR_OF_DAY));
        buffer.append(TIME_SEPARATOR);
        buffer.append(calendar.get(Calendar.MINUTE));

        return buffer.toString();
    }

    private void resetBuffer() {
        buffer.delete(0, buffer.length());
    }
}
